package cn.zut.hhr.class1.service.impl;

import cn.zut.hhr.class1.entity.SysUser;
import cn.zut.hhr.class1.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class SysSecurityService implements UserDetailsService {
    @Autowired
    private ISysUserService iSysUserService;
    //自定义登录认证过程,根据用户名获取用户信息，如果用户被锁定，则抛出异常
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getUsername,username);
        SysUser sysUser = iSysUserService.getOne(lambdaQueryWrapper);
        if (null == sysUser){
            throw new UsernameNotFoundException("用户名不存在");
        }
        else if (!sysUser.getEnabled())
            throw new LockedException("用户被锁定");
        return sysUser;
    }
}
